-- 创建目标周围怪物检测框架
local rangeDetectorFrame = CreateFrame("Frame", "RangeDetectorFrame", UIParent)
rangeDetectorFrame:SetSize(200, 80)
rangeDetectorFrame:SetPoint("CENTER", 0, 100)

-- 设置背景
local bg = rangeDetectorFrame:CreateTexture(nil, "BACKGROUND")
bg:SetAllPoints(true)
bg:SetColorTexture(0.1, 0.1, 0.2, 0.7)

-- 设置边框
local border = rangeDetectorFrame:CreateTexture(nil, "BORDER")
border:SetPoint("TOPLEFT", -3, 3)
border:SetPoint("BOTTOMRIGHT", 3, -3)
border:SetColorTexture(0.3, 0.3, 0.5, 0.5)

-- 标题文本
local title = rangeDetectorFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
title:SetPoint("TOP", 0, -10)
title:SetText("目标周围怪物检测")
title:SetTextColor(1, 0.8, 0)

-- 状态文本
local statusText = rangeDetectorFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
statusText:SetPoint("CENTER", 0, 10)
statusText:SetText("无目标")
statusText:SetTextColor(0.8, 0.8, 0.8)

-- 附近怪物计数文本
local countText = rangeDetectorFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
countText:SetPoint("BOTTOM", 0, 10)
countText:SetText("附近怪物: 0")
countText:SetTextColor(0.8, 0.8, 0.8)

-- 检测函数
local function CheckNearbyEnemies()
    if not UnitExists("target") then
        statusText:SetText("无目标")
        statusText:SetTextColor(0.8, 0.8, 0.8)
        countText:SetText("附近怪物: 0")
        return 0
    end

    local targetName = UnitName("target")
    statusText:SetText("目标: "..targetName)

    -- 获取目标位置
    local tx, ty, tz = UnitPosition("target")
    if not tx then
        statusText:SetText("无法获取目标位置")
        statusText:SetTextColor(1, 0, 0)
        countText:SetText("附近怪物: 0")
        return 0
    end

    local nearbyCount = 0

    -- 遍历所有单位
    for i = 1, 100 do
        local unit = "nameplate"..i
        if UnitExists(unit) then
            -- 检查单位是否可攻击
            if UnitCanAttack("player", unit) and not UnitIsPlayer(unit) then
                -- 获取单位位置
                local ux, uy, uz = UnitPosition(unit)
                if ux then
                    -- 计算距离
                    local dx = tx - ux
                    local dy = ty - uy
                    local dz = tz - uz
                    local distance = math.sqrt(dx*dx + dy*dy + dz*dz)

                    -- 检查是否在8码范围内
                    if distance <= 8 then
                        nearbyCount = nearbyCount + 1
                    end
                end
            end
        else
            -- 当遇到不存在的单位时停止循环
            break
        end
    end

    -- 更新显示
    countText:SetText("附近怪物: "..nearbyCount)

    if nearbyCount > 0 then
        statusText:SetTextColor(0, 1, 0) -- 绿色：有附近怪物
    else
        statusText:SetTextColor(1, 0, 0) -- 红色：无附近怪物
    end

    return nearbyCount
end

-- 设置更新频率
local updateInterval = 0.5
local elapsed = 0

rangeDetectorFrame:SetScript("OnUpdate", function(self, delta)
    elapsed = elapsed + delta
    if elapsed < updateInterval then return end
    elapsed = 0

    CheckNearbyEnemies()
end)

-- 添加拖动功能
rangeDetectorFrame:SetMovable(true)
rangeDetectorFrame:EnableMouse(true)
rangeDetectorFrame:SetClampedToScreen(true)
rangeDetectorFrame:RegisterForDrag("LeftButton")

rangeDetectorFrame:SetScript("OnDragStart", rangeDetectorFrame.StartMoving)
rangeDetectorFrame:SetScript("OnDragStop", rangeDetectorFrame.StopMovingOrSizing)

-- 初始检测
CheckNearbyEnemies()

-- 打印说明
print("目标周围怪物检测框已创建")
print(" - 拖动：鼠标左键")
print(" - 显示目标周围8码内可攻击怪物数量")